using System.ComponentModel;
using System.IO;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SharePointing.Features.MetaTags.Controls
{
    /// <summary>
    /// This control can be used to define MetaTags directly in the page
    /// using the familiar, simple methodology (like standard server controls)
    /// 
    /// USAGE:
    /// Register the TagPrefix on the page: <%@ Register Tagprefix="MetaTagWebControls" Namespace="SharePointing.Features.MetaTags" Assembly="SharePointing.Features.MetaTags, Version=1.0.0.0, Culture=neutral, PublicKeyToken=eaf5ef06195f8257" %>
    /// use them directly like this:
    ///     <MetaTagWebControls:MetaTag ID="MetaTag1" runat="server" Name="author">Joe Blow</MetaTagWebControls:MetaTag>
    ///     <MetaTagWebControls:MetaTag ID="MetaTag2" runat="server" Name="Classification">Business to Business, B2B, Manufacturing, Industrial Supplies, Machinery and Tools, Machine Tools, Cutting Tools, Wire Strippers</MetaTagWebControls:MetaTag>
    ///     <MetaTagWebControls:MetaTag ID="MetaTag3" runat="server" Name="keywords"><asp:Literal ID="keywordsLiteral" runat="server"></asp:Literal></MetaTagWebControls:MetaTag>
    /// 
    /// OR use the Content Placeholder in the MasterPage like this:
    /// <asp:ContentPlaceHolder runat="server" id="PlaceHolderPageMetaTags"></asp:ContentPlaceHolder>
    /// 
    /// and then add the tags to the Content Placeholder in your page layout
    /// <asp:Content ContentPlaceHolderID="PlaceHolderPageMetaTags" runat="server">
    /// <MetaTagWebControls:MetaTag runat="server" name="title" id="titleMetaTag"><SharePointWebControls:FieldValue id="titleTag" FieldName="Title" runat="server"/></MetaTagWebControls:MetaTag>
    /// <MetaTagWebControls:MetaTag runat="server" name="description" id="descriptionMetaTag"><SharePointWebControls:FieldValue id="commentsTag" FieldName="Comments" runat="server"/></MetaTagWebControls:MetaTag>
    /// </asp:Content>
    /// </summary>
    [ParseChildren(false), ToolboxData("<{0}:MetaTag runat=\"server\" name=\"\"> </{0}:MetaTag>"), PersistChildren(true),
    AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal),
    AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal),
    DefaultProperty("Name")]
    public class MetaTag : WebControl
    {
        protected string m_Name = "";
        
        public MetaTag() : base(HtmlTextWriterTag.Meta)
        {
        
        }

        
        [Bindable(true), Category("Appearance"), DefaultValue(""), Description("Sets the MetaTag's name attribute.")]
        public string Name
        {
            get { return m_Name; }
            set { m_Name = value; }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            // Render control only if there is a tagname specified
            if (!m_Name.Equals(string.Empty))
                base.Render(writer);
        }
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            // Write the name attribute
            writer.AddAttribute("name", m_Name);

            // Write the value attribute
            StringWriter sw = new StringWriter();
            base.RenderContents(new HtmlTextWriter(sw));
            writer.AddAttribute("value", sw.ToString());
            // the metata does not have any other attributes that could be rendered: don't call base...
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            // this control does not render any contents
        } 

    }
}
